
import { createRouter, createWebHistory } from 'vue-router'

import Home from './views/Home.vue'
import Login from './views/Login.vue'
import Column from './views/ColumnDetail.vue'
import CreatePost from './views/CreatePost.vue'
import store from './store'
const routerHistory = createWebHistory()
const router = createRouter({
  history: routerHistory,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta:{redirectAlreadyLogin:true}
    },
    {
      path: '/column/:id',
      name: 'column',
      component: Column
    },
    {
      path: '/create',
      name: 'create',
      component: CreatePost,
      meta:{ requiredLogin :true}  //改路由是否登录
    }
  ]
})


// to 去哪里  from 从哪个路由去
router.beforeEach((to,from,next)=>{
  if(to.meta.requiredLogin && !store.state.user.isLogin){
   next({name:'login'})
  }else if(to.meta.redirectAlreadyLogin && store.state.user.isLogin){
    next('/')
  }
  else{
    next()
  }
   
})
export default router